import pathToRegexp from 'path-to-regexp/index';
import {getPrivilege,postPrivilege,getPrivilegeTow,getAllRoleOne,getAllRoleTow,getMaillist,getprivilegeList,postPrivilegeTow} from 'services/app';
import { message} from 'antd';

export default {
    namespace: 'privilege',
    state: {
        pages:1,
        record:{},
        activeKey:'2',
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getPrivilege ({ payload }, { call, put }){
            const {data,total} = yield call(getPrivilege, payload);
            yield put({ type: 'save', payload: {
                getPrivilegeData:data,
                total:total,
                pages:payload.pageNumber,
                view:false,
            }});
        },
        //获取公告
        *getPrivilegeTow ({ payload }, { call, put }){
            const {data,total} = yield call(getPrivilegeTow, payload);
            yield put({ type: 'save', payload: {
                getPrivilegeData:data,
                total:total,
                pages:payload.pageNumber,
                view:false,
            }});
        },
        //获取角色
        *getAllRole ({ payload }, { call,put }){
            // const {data} = yield call(getAllRoleOne, {o_id:'111111'});//部门
            const {data} = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            const dataTow = yield call(getAllRoleTow, {o_id:'222222'});//角色
            yield put({ type: 'save', payload: {getBMZZData:dataTow.data,getMaillist:data} });
        },
        //下发通知
        *postPrivilege ({ payload }, { call, put }){
            const data = yield call(postPrivilege, payload);
            if(data.code == 0){
                message.success("下发成功！");
                yield put({ type: 'getPrivilege', payload: {
                    title:'',
                    pageNumber :1,
                    pageSize :10,
                }});
            }
        },
        //发送公告
        *postPrivilegeTow  ({ payload }, { call, put }){
            const data = yield call(postPrivilegeTow, payload);
            if(data.code == 0){
                message.success("下发成功！");
                yield put({ type: 'getPrivilegeTow', payload: {
                    titleText:'',
                    startTime:"",
                    endTime:"",
                    pageNumber :1,
                    pageSize :10,
                }});
            }
        },
        //详情
        *getprivilegeList ({ payload }, { call, put }){
            const {data} = yield call(getprivilegeList, payload);
            yield put({ type: 'save', payload: {listData:data[0]}});
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
